<?php
// +----------------------------------------------------------------------+
// | The CompanyName 星座女神                                             |
// +----------------------------------------------------------------------+
// | Copyright (c) 2016, CompanyName 星座女神. All rights reserved.       |
// +----------------------------------------------------------------------+
// | Authors: CompanyName 星座女神.                                       |
// |                                                                      |
// +----------------------------------------------------------------------+
/**
 * @version  2.2
 * @author   guokunsheng
 * @date     2017-07-012
 */
namespace App\Http\Controllers\Api\v1\Astro;
use App\Http\Controllers\Api\v1\Astro\PlanetpowerController;
use App\Http\Controllers\Controller;// 基础控制器
class HousepowerController extends Controller
{
    private $base_data = [];

    public function __construct($data = [])
    {
        if (empty($data)) if (empty($data)) return ['status' => '10001', 'Invalid Data!'];
        $this->base_data = $data;
    }

    public function _initload()
    {
        $data1  = $this->getResult(1,$this->house_influence($this->base_data['houses'][1]));
//        $data2  = $this->getResult(2,$this->house_influence($this->base_data['houses'][2]));
//        $data3  = $this->getResult(3,$this->house_influence($this->base_data['houses'][3]));
//        $data4  = $this->getResult(4,$this->house_influence($this->base_data['houses'][4]));
//        $data5  = $this->getResult(5,$this->house_influence($this->base_data['houses'][5]));
//        $data6  = $this->getResult(6,$this->house_influence($this->base_data['houses'][6]));
//        $data7  = $this->getResult(7,$this->house_influence($this->base_data['houses'][7]));
//        $data8  = $this->getResult(8,$this->house_influence($this->base_data['houses'][8]));
//        $data9  = $this->getResult(9,$this->house_influence($this->base_data['houses'][9]));
//        $data10  = $this->getResult(10,$this->house_influence($this->base_data['houses'][10]));
//        $data11  = $this->getResult(11,$this->house_influence($this->base_data['houses'][11]));
//        $data12  = $this->getResult(12,$this->house_influence($this->base_data['houses'][12]));
//        echo '1宫：'. $data1 ."\n";
//        echo '2宫：'. $data2 ."\n";
//        echo '3宫：'. $data3 ."\n";
//        echo '4宫：'. $data4 ."\n";
//        echo '5宫：'. $data5 ."\n";
//        echo '6宫：'. $data6 ."\n";
//        echo '7宫：'. $data7 ."\n";
//        echo '8宫：'. $data8 ."\n";
//        echo '9宫：'. $data9 ."\n";
//        echo '10宫：'. $data10 ."\n";
//        echo '11宫：'. $data11 ."\n";
//        echo '12宫：'. $data12 ."\n";
        //dd($this->house_influence($this->base_data['houses'][1]));
    }

    public function house_influence($house = [],$alike_sign = 0.25,$alike_house = 0.15,$flu_house = 0.4,$flu_sign = 0.3)
    {
        $Planetpower = new PlanetpowerController($this->base_data);
        $flag = [1=>4,2=>3,3=>2,4=>1,5=>0,6=>2,7=>3,8=>9,9=>5,10=>6,11=>7,12=>8];// 行星对应元素标号
        $sc = [1=>0,2=>0,3=>0,4=>0,5=>0,6=>0,7=>0,8=>0,9=>0,10=>0,11=>0,12=>0];
        if($house['have_planet_num'] == 0)
        {
            $house_start_coef = 1;
        }else{
            $house_start_coef = 0.75;

            foreach ($house['in_house_area_planet_info'] as $pl)
            {
                if($pl['id'] > 9) continue;
                $coef = 1;
                $ph_num = 0;
                if(!empty($this->base_data['planets'][$pl['id']]['phase']))
                {
                    foreach ($this->base_data['planets'][$pl['id']]['phase'] as $ph)
                    {
                        if($ph['id'] < 10) $ph_num++;
                    }
                }
                if(empty($this->base_data['planets'][$pl['id']]['phase']) || $ph_num == 0)
                {
                    $coef = 1.3;
                }
                if($house['in_sign_id'] != $this->base_data['planets'][$pl['id']]['in_sign_id'])
                {
                    $coef -= 0.1;
                }
                for($i=1;$i<13;$i++)
                {
                    if($flag[$i] == $pl['id'])
                    {
                        $sc[$i] += $coef;
                        $p_sc = $Planetpower->planet_influence($this->base_data['planets'][$pl['id']],
                            $alike_sign,$alike_house,$flu_sign,$flu_house);
                        foreach ($p_sc as $j => $ps)
                        {
                            $sc[$j] += $coef * $ps;
                        }
                    }
                }
                $sc[$this->base_data['planets'][$pl['id']]['in_sign_id']] += $flu_sign * $coef;
                $sc[$this->base_data['planets'][$pl['id']]['in_house_id']] += $flu_house * $coef;
            }
        }
        $sc[$house['in_sign_id']] += $house_start_coef;
        $h_pl_id = config('astrolog.protect_id')[$house['in_sign_id']-1];// 宫主星id
        $h_p_sc = $Planetpower->planet_influence($this->base_data['planets'][$h_pl_id],$alike_sign,$alike_house,$flu_sign,$flu_house);
        foreach ($h_p_sc as $i => $hpsc)
        {
            $sc[$i] += $house_start_coef * $hpsc;
        }
        return $sc;
    }

    /**
     * 附加：宫位指数分析版本
     * 1、    计算该宫位各元素指数；
     * 2、    该宫是第几宫，则第几号元素指数乘以60；
     * 3、    若命宫星座是X号元素，且X不是宫位号，则X号指数*35；
     * 4、    若非4宫，且X不等于4，4号指数*30；
     * 5、    若非5宫，且X不等于5，5号指数*35；
     * 6、    其它各号元素指数相加后*20
     * 7、    2-6项相加，大于500则令其=500
     * @param null $hid
     * @param array $sc
     * @return null
     */
    private function getResult($hid = null,$sc = [])
    {
        if($hid === null || empty($sc)) return null;
//        p($hid);
//        p($sc);
        $id_temp = [$hid];
        $sc[$hid] *= 60;
        $t = $this->base_data['houses'][1]['in_sign_id'];
        if($t != $hid)
        {
            $id_temp[] = $t;
            $sc[$t] *= 35;
        }
        if($hid != 4 && $t != 4)
        {
            $id_temp[] = 4;
            $sc[4] *= 30;
        }
        if($hid != 5 && $t != 5)
        {
            $id_temp[] = 5;
            $sc[5] *= 35;
        }
        $total = 0;
        $total_other = 0;
        foreach ($sc as $i => $s)
        {
            if(!in_array($i,$id_temp))
            {
                $total_other += $s;
            }else{
                $total += $s;
            }
        }
        $result = $total + $total_other * 20;
//        if($result > 500 ) $result = 500;

        return round($result);
    }

}